package utilities;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/** Class for creating Dom Tree for a XML file
 * @author DarkoLaptop2
 *
 */
public class DomTree {
	/** This method is a constructor of the class
	 */
	public DomTree(String fileName) {
		super();
		this.fileName = fileName;
		//this.fXmlFile = DomTree.class.getResourceAsStream(fileName);
	}
	
	private String fileName;
	private InputStream fXmlFile;
	private Document doc;
	DocumentBuilderFactory dbFactory;
	DocumentBuilder dBuilder;
	
	/** This method is used for parsing an XML file with previous given name of the file
	 * and creating a new Document
	 * @return Document 
	 * @throws FileNotFoundException
	 */
	public Document ParseXml() throws FileNotFoundException
	{
		//fXmlFile = DomTree.class.getResourceAsStream(fileName);
		fXmlFile = new FileInputStream(fileName);
		dbFactory = DocumentBuilderFactory.newInstance();
		try {
			dBuilder = dbFactory.newDocumentBuilder();
			doc = dBuilder.parse(fXmlFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			}
		return doc;
	}
}
